package compoundDB.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import compoundDB.database.container.*;

@RunWith(Suite.class)

@SuiteClasses({
				DBManagerTest.class,
				CachedQueryReaderTest.class,
				ColumnFieldTest.class,
				SearchOptionTest.class
				})

/**
 * This class provides a few utility methods to save copy-pasting code in other database test classes.
 */
public class DatabaseTests
{
	public static void populateDatabase() throws SQLException
	{
		Connection conn = getNewConnection();
		Statement st = conn.createStatement();
		st.executeUpdate("INSERT INTO compounds VALUES(0, 2, 'tags2', 'smiles2', NULL, 'links2', 'notes2');");
		st.executeUpdate("INSERT INTO compounds VALUES(0, 3, 'tags3', 'smiles3', NULL, 'links3', 'notes3');");
		st.executeUpdate("INSERT INTO compounds VALUES(0, 4, 'tags4', 'smiles4', NULL, 'links4', 'notes4');");
		st.executeUpdate("INSERT INTO compounds VALUES(0, 5, 'tags5', 'smiles5', NULL, 'links5', 'notes5');");
		st.executeUpdate("INSERT INTO compound_names VALUES(2, 'comp21')");
		st.executeUpdate("INSERT INTO compound_names VALUES(2, 'comp22')");
		st.executeUpdate("INSERT INTO compound_names VALUES(3, 'comp31')");
		st.executeUpdate("INSERT INTO compound_names VALUES(4, 'comp41')");
		st.executeUpdate("INSERT INTO compound_names VALUES(4, 'comp42')");
		st.executeUpdate("INSERT INTO compound_names VALUES(4, 'comp43')");
		st.executeUpdate("INSERT INTO compound_names VALUES(5, 'comp51')");
		st.close();
		conn.close();
	}
	
	public static Connection getNewConnection() throws SQLException
	{
		String url = DBConfig.getConnectUrl() + "//" + DBConfig.getConnectHost() + ":"
		+ DBConfig.getConnectPort() + "/" + DBConfig.getCompoundDatabase();
		
		return DriverManager.getConnection(url, DBConfig.getUser(),
				DBConfig.getPassword());
	}
	
	public static void setupTestDatabase() throws ClassNotFoundException,
			SQLException
	{
		Class.forName(DBConfig.getDatabaseLoadUrl());
		
		DBConfig.setCompoundDatabase("compounddbTest");
		
		String[] setupQueries = {
				"DROP TABLE IF EXISTS compounds;",
				"DROP TABLE IF EXISTS compound_names;",
				"DROP TABLE IF EXISTS users;",
				"CREATE TABLE compounds (guid BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, ownerid BIGINT NOT NULL DEFAULT 0, tags TEXT, smiles TEXT, structure BLOB, links TEXT, notes LONGTEXT);",
				"CREATE TABLE compound_names (guid BIGINT NOT NULL, compound_name TEXT NOT NULL, INDEX(guid), INDEX(guid));",
				"CREATE TABLE users (guid BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, user_name TEXT, pass_hash TEXT, visible_name TEXT);",
				"INSERT INTO compounds (tags, smiles, links, notes) VALUES ('test', 'C-Test', 'www.test.net', 'test notes here');",
				"INSERT INTO compound_names VALUES (1, 'Test Compound');"};
		
		
		Connection connection = getNewConnection();
		Statement st = connection.createStatement();
		for (int x = 0; x < setupQueries.length; x++)
		{
			st.executeUpdate(setupQueries[x]);
		}
		
		st.close();
		
		connection.close();
	}
}
